﻿@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
------------------------------------------------------------*@

@page "/endpoints"
@page "/endpoints/{discovererId}/{applicationId}"
@page "/endpoints/{discovererId}/{applicationId}/{supervisorId}"

@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.App.Services;
@using Microsoft.Azure.IIoT.App.Components.Buttons;
@using Microsoft.Azure.IIoT.App.Components.Drawer;
@using Microsoft.Azure.IIoT.App.Components.ErrorMessage;
@using Microsoft.Azure.IIoT.App.Components.Loader;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry.Models;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry;
@using Microsoft.Azure.IIoT.App.Common;
@using Microsoft.Azure.IIoT.App.Models;

@inject NavigationManager NavigationManager
@inject IRegistryServiceApi RegistryService
@inject RegistryServiceEvents RegistryServiceEvents
@inject Registry RegistryHelper
@inject UICommon CommonHelper
@inherits IIoTItemsCollection<EndpointInfo>

<h1>Endpoints</h1>

@if (IsIdGiven(DiscovererId))
{
    <div class="subtitle">
        DiscovererId: @DiscovererId
    </div>
}

@if (IsIdGiven(ApplicationId))
{
    <div class="subtitle">
        AssetUri: @CommonHelper.ApplicationUri[ApplicationId]
    </div>
}

<table class="table table-responsive @_tableView">
    <thead>
        <tr>
            <th class="width-large">Endpoint Url</th>
            <th class="width-medium-large">Security Mode - Policy</th>
            <th class="width-small">Security Level</th>
            <th class="width-small">Endpoint State</th>
            <th class="width-medium">Activation Status</th>
            <th class="width-small">Activation Action</th>
            <th class="width-small"></th>
            <th class="width-small"></th>

            @if (IsIdGiven(DiscovererId))
            {
                <th class="width-xsmall">
                    @{var link = @NavigationManager.BaseUri + "discoverers";}
                    <a aria-label="back" href="@link">
                        <span class="oi oi-arrow-left"></span>
                    </a>
                </th>
            }
            else if (IsIdGiven(ApplicationId))
            {
                <th class="width-xsmall">
                    @{var link = @NavigationManager.BaseUri + "applications";}
                    <a aria-label="back" href="@link">
                        <span class="oi oi-arrow-left"></span>
                    </a>
                </th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var endpoint in Items.Results)
        {
            <tr class="@GetEndpointVisibilityString(endpoint)">
                @if (endpoint.EndpointModel.EndpointState == EndpointConnectivityState.Ready ||
        endpoint.EndpointModel.EndpointState == EndpointConnectivityState.Unauthorized)
                {
                    <td class="hover-text width-large">
                        <div>
                            @{
                                string discover = DiscovererId ?? RegistryHelper.PathAll;
                                string application = ApplicationId ?? RegistryHelper.PathAll;
                                string supervisor = SupervisorId ?? RegistryHelper.PathAll;
                            }
                            <a href='browser/@discover/@application/@supervisor/@endpoint.EndpointModel.Registration.Id'>
                                @endpoint.EndpointModel.Registration.Endpoint.Url
                            </a>
                        </div>
                    </td>
                }
                else
                {
                    <td class="hover-text width-large">
                        <div>
                            @endpoint.EndpointModel.Registration.Endpoint.Url
                        </div>
                    </td>
                }
                <td class="hover-text width-medium-large">
                    <div>
                        @endpoint.EndpointModel.Registration.Endpoint.SecurityMode - @CommonHelper.ExtractSecurityPolicy(endpoint.EndpointModel.Registration.Endpoint.SecurityPolicy)
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        @endpoint.EndpointModel.Registration.SecurityLevel
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        @(endpoint.EndpointModel.EndpointState?.ToString() ?? "Disconnected")
                    </div>
                </td>
                <td class="hover-text width-medium">
                    <div>
                        @endpoint.EndpointModel.ActivationState.ToString()
                    </div>
                </td>
                <td class="width-small">
                    @{string activationStatus = IsEndpointActivated(endpoint) == true ? "Turn off" : "Turn on";}
                    @{bool isDisabled = !IsEndpointSeen(endpoint);}
                    <button class="btn btn-primary" disabled="@isDisabled" @onclick="(() => SetActivationAsync(endpoint))">@activationStatus</button>
                </td>
                <td class="hover-text width-small">
                    <div>
                        <a class="ellipsis-space" href="javascript: void(0)" @onclick="@(() => OpenDrawer(endpoint))">More Data</a>
                    </div>
                </td>
                @if (IsEndpointSeen(endpoint) && IsEndpointActivated(endpoint))
                {
                    <td class="width-small space-wrap">
                        <div>
                            <a class="ellipsis-space" href='publishedNodes/@endpoint.EndpointModel.Registration.Id/@DiscovererId/@ApplicationId/@SupervisorId'>Published Nodes</a>
                        </div>
                    </td>
                }
                else
                {
                    <td class="width-small"></td>
                }
                @if (IsIdGiven(DiscovererId) || IsIdGiven(ApplicationId))
                {
                    <td class="width-small"></td>
                }
            </tr>
        }
    </tbody>
</table>
<div class="@_tableEmpty center">No Endpoints found.</div>

<Drawer HeaderText="Endpoint Info" ObjectData="@EndpointData" IsOpen="@IsOpen" CloseDrawer="@(() => CloseDrawer())">
    <_DrawerEndpointContent EndpointData="@EndpointData"></_DrawerEndpointContent>
</Drawer>

<ErrorMessage PageError="@Items.Error" Status="@Status"></ErrorMessage>

<Spinner IsLoading=@IsLoading></Spinner>

<LoadMore Result=@Items IsLoading=@IsLoading LoadMoreItems=@(async() => await LoadMoreItems()) />
